print('im lua')
mousx = 0
mousy = 0
oldmousx = 0
oldmousy = 0
ang = 0
numframes = 0
deltatime = 0


Color = {}

colors = {}
local colorsmeta = {}

function Color.new(r,g,b)
  local info = {}
  info.r = r
  info.g = g
  info.b = b
  setmetatable(info, colorsmeta)
  return info
end

particles = {}
Particle = {}
local particlemeta = {}
function Particle.new(x,y)
  local info = {}
  info.x = x
  info.y = y
  info.dx = math.random(50)-25
  info.dy = math.random(50)-25
  info.color = Color.new(math.random(255),math.random(255),math.random(255) )
  --setmetatable(info, particlemeta )
  return info
end

---colorsmeta.__index = Color


function dist(x1,y1,x2,y2)
  return (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)
end

function setup()
end

function draw()
  setcolor(255,255,255)
  bmxprintf(10, 10, "numframes " .. numframes)
  bmxprintf(10, 20, "particles: " .. #particles)
  bmxprintf(10, 30, "dt: " .. deltatime)

  for i = 1, #particles do
    setcolor( particles[i].color.r, particles[i].color.g, particles[i].color.b )
    drawsph( particles[i].x, particles[i].y, 5 )

    particles[i].x = particles[i].x + particles[i].dx * deltatime
    particles[i].y = particles[i].y + particles[i].dy * deltatime
  end

  table.remove(particles, 1)


  table.remove(colors, 1)

  if mousx ~= oldmousx or mousy ~= oldmousy  then
    table.insert(particles,
      Particle.new( mousx + math.random(20) -10, mousy + math.random(20) -10  ))

    table.insert(particles,
      Particle.new( mousx + math.random(20) -10, mousy + math.random(20) -10  ))

    table.insert(particles,
      Particle.new( mousx + math.random(20) -10, mousy + math.random(20) -10  ))

    table.insert(particles,
      Particle.new( mousx + math.random(20) -10, mousy + math.random(20) -10  ))

    table.insert(particles,
      Particle.new( mousx + math.random(20) -10, mousy + math.random(20) -10  ))

    table.insert(particles,
      Particle.new( mousx + math.random(20) -10, mousy + math.random(20) -10  ))

    table.insert(particles,
      Particle.new( mousx + math.random(20) -10, mousy + math.random(20) -10  ))

    table.insert(particles,
      Particle.new( mousx + math.random(20) -10, mousy + math.random(20) -10  ))

  end

  oldmousx = mousx
  oldmousy = mousy

--[[
  local tx = mousx; local ty = mousy;
  local oldx = math.cos(ang)*50
  local oldy = math.sin(ang)*50

  for i = 0,8 do
    phi = ang+i * 2 * 3.14 /7
    local x = math.cos( phi )* 50
    local y = math.sin( phi ) * 50
    drawline( tx+oldx, ty+oldy, tx+x, ty+y )
    oldx = x
    oldy = y
  end
  ]]
  ang = ang + 0.05
  numframes = numframes + 1

end
